package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Systemdictionaryitem;
import cn.wolfcode.quest.CustomerQueryObject;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.quest.QueryObject;
import cn.wolfcode.quest.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemdictionaryitemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemdictionaryitemService systemdictionaryitemService;


    @RequiresPermissions(value = {"customer:potentialList","潜在客户列表"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        //只取得潜在的客户
        qo.setStatus(0);
        Subject subject = SecurityUtils.getSubject();
        //如果登陆的用户不是管理员或者经理则只能查找自己的潜在用户
        if(!(subject.hasRole("admin")|| subject.hasRole("Market_Manager"))){
            Employee principal = (Employee)subject.getPrincipal();
            qo.setSellerId(principal.getId());
        }
        //找到角色为市场人员或者是市场经理的员工用户
        List<Employee> employees = employeeService.selRolesBySn("Market", "Market_Manager");
        model.addAttribute("employees",employees);
        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageResult", pageInfo);
        //从字典明细中找到所有的工作
      // List<Systemdictionaryitem> jobs=systemdictionaryitemService.selectByParentId(1L);
        List<Systemdictionaryitem> jobs=systemdictionaryitemService.selectBySn("job");
        //从字典明细中找到所有的客户来源
       // List<Systemdictionaryitem> sources = systemdictionaryitemService.selectByParentId(2L);
        List<Systemdictionaryitem> sources=systemdictionaryitemService.selectBySn("source");

        //从字典明细中找到所有的与客户的交流方式
        List<Systemdictionaryitem> ccts = systemdictionaryitemService.selectBySn("communicationMethod");
        model.addAttribute("ccts",ccts);
        model.addAttribute("jobs",jobs);
        model.addAttribute("sources",sources);
        return "potentialList/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }
}
